<?php
namespace app\index\controller;

use app\common\controller\Frontend;
use think\Session;
use think\helper\Time;
use app\admin\model\Osideentry as mOsideentry;
use think\Config;
use think\Db;
use app\admin\model\Outsideschool as OutsideSchool;

use app\admin\model\Outsidestudent as Outsidestudent;

class Osideentry extends Frontend
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = 'weiui';
    protected $model = null;



    public function _initialize()
    {

        parent::_initialize();
   	//die();
	$this->model = new mOsideentry();
    }

    public function index($ids=1)
    {
         if($this -> request -> isPost()){
            $params = $this->request->post("row/a");
            $params['create_time'] = time();
            $gateName = Config::get('gate')[$params['gate_id']];
            $params['gate_name'] = $gateName;
            $res = $this->model->allowField(true)->save($params);
            if ($res)
                $this->success('登记成功','',['nickname'=>$params['nickname'],'create_time'=>date("Y年m月d日 H时i分s秒",$params['create_time'])]);
            else
            {
                $this->error('登记失败');
                // $from_storage = 1;
            }
        }
        $this -> assign('ids',$ids);
        $title = '校外人员入校登记';
        $this -> assign('pageTitle',$title);
        return $this -> fetch();
    }

    public function check($ids=1)
    {
        if($this -> request -> isPost()){
            $params = $this->request->post("row/a");
            $tel = $params['tel'];

            //白名单
//            if ($re = $this->whitelist($tel)){
//                $this->success('登记成功','',['nickname'=>$re['nickname'],'admission_time'=>$re['admission_time']]);
//            }
            $res = Db::name('white_list') -> where(['mobile' => $tel,'status' => 1]) -> find();
            if(!empty($res)){
                $this->success('登记成功','',['nickname'=>$res['nickname'],'admission_time'=>date('Y-m-d')]);
            }
            $out_model = new Outsideschool();
            //黑名单
            $info = $out_model->where(['tel'=>$params['tel']])->find();
            if(!empty($info))
            {
                //不在黑名单 返回 true
                if (!$this->blacklist($info['nickname']))
                {
                    $this->error('你好，此用户不允许进入');
                }
            }
            $where = [];
            $where['admission_starttime']  = ['<=',date('Ymd')];
            $where['admission_endtime']  = ['>=',date('Ymd')];
//            $where['admission_time']  = date('Ymd');
            $where['tel']  = $tel;
            $info = $out_model->where($where)->find();

            // 校外学生预约查询
            $outstuent_model = new Outsidestudent();
            $where_student = [];
            $where_student['tel']  = $tel;
            $where_student['admission_time'] = strtotime(date('Y-m-d'));
            $info_student = $outstuent_model->where($where_student)->find();

            if ($info){
                $where['is_check'] = 1;
                $pass = $out_model->where($where)->find();
                if (!$pass){
                    if($info['is_check'] == 0){
                        $this->error('申请未审核，不允许通行');
                    }elseif($info['is_check'] == 2){
                        $this->error('申请被驳回，不允许通行');
                    }else{
                        $this->error('无今日入校申请记录，不允许通行');
                    }
                }
                $updata = [];
                $updata['is_use'] = 1;
                $updata['use_time'] = time();
                $updata['gate_id'] = $params['gate'];
                $updata['gate_name'] = isset(OutsideSchool::gate[$params['gate']])?OutsideSchool::gate[$params['gate']]:'';
                if($info->use_time_record){
                    $useTimeRecord = json_decode($info->use_time_record,true);
                    $useTimeRecord[] = $updata['use_time'];
                    $updata['use_time_record'] = json_encode($useTimeRecord);
                }else{
                    $updata['use_time_record'] = json_encode([$updata['use_time']]);
                }

                //记录为已进入
                $res = $out_model->save($updata,['id' => $info['id']]);
                if ($res === false){
                    $this->error('登记失败');
                }
                $this->success('登记成功','',['nickname'=>$info['nickname'],'admission_time'=>$info['admission_time']]);
            }else if($info_student){
                if($info_student['status'] == 0){
                    $this->error('预约未审核，不允许通行');
                }elseif($info_student['status'] == 2){
                    $this->error('预约被驳回，不允许通行');
                }
                // 进入时间
                $updata = [];
                $updata['is_goin'] = 1;
                $updata['into_time'] = time();
                $outstuent_model->save($updata,['id' => $info_student['id']]);
                $this->success('登记成功','',['nickname'=>$info_student['nickname'],'admission_time'=>$info_student['admission_time_text']]);
            }else{
                $this->error('无今日入校申请记录，不允许通行');
            }
        }
        $this -> assign('ids',$ids);
        $this -> assign('gate',input('gate',''));
//        exit(input('gate',''));
        $title = '校外人员入校登记';
        $this -> assign('pageTitle',$title);
        return $this -> fetch();
    }

    public function check1($ids=1)
    {
        if($this -> request -> isPost()){
            $params = $this->request->post("row/a");
            $tel = $params['tel'];
            //白名单
//            if ($re = $this->whitelist($tel)){
//                $this->success('登记成功','',['nickname'=>$re['nickname'],'admission_time'=>$re['admission_time']]);
//            }
            $res = Db::name('white_list') -> where(['mobile' => $tel,'status' => 1]) -> find();
            if(!empty($res)){
                $this->success('登记成功','',['nickname'=>$res['nickname'],'admission_time'=>date('Y-m-d')]);
            }
            $out_model = new Outsideschool();
            //黑名单
            $info = $out_model->where(['tel'=>$params['tel']])->find();
            if(!empty($info))
            {
                if (!$this->blacklist($info['nickname']))
                {
                    $this->error('你好，此用户不允许进入');
                }
            }
            $where = [];
            $where['admission_starttime']  = ['<=',date('Ymd')];
            $where['admission_endtime']  = ['>=',date('Ymd')];
//            $where['admission_time']  = date('Ymd');
            $where['tel']  = $tel;
            $info = $out_model->where($where)->find();

            // 校外学生预约查询
            $outstuent_model = new Outsidestudent();
            $where_student = [];
            $where_student['tel']  = $tel;
            $where_student['admission_time'] = strtotime(date('Y-m-d'));
            $info_student = $outstuent_model->where($where_student)->find();

            if ($info){
                $where['is_check'] = 1;
                $pass = $out_model->where($where)->find();
                if (!$pass){
                    if($info['is_check'] == 0){
                        $this->error('申请未审核，不允许通行');
                    }elseif($info['is_check'] == 2){
                        $this->error('申请被驳回，不允许通行');
                    }else{
                        $this->error('无今日入校申请记录，不允许通行');
                    }
                }
                $updata = [];
                $updata['is_use'] = 1;
                $updata['use_time'] = time();
                $updata['gate_id'] = $params['gate'];
                $updata['gate_name'] = isset(OutsideSchool::gate[$params['gate']])?OutsideSchool::gate[$params['gate']]:'';

                //记录为已进入
                $res = $out_model->save($updata,['id' => $info['id']]);
                if ($res === false){
                    $this->error('登记失败');
                }
                $this->success('登记成功','',['nickname'=>$info['nickname'],'admission_time'=>$info['admission_time']]);
            }else if($info_student){
                if($info_student['status'] == 0){
                    $this->error('预约未审核，不允许通行');
                }elseif($info_student['status'] == 2){
                    $this->error('预约被驳回，不允许通行');
                }
                // 进入时间
                $updata = [];
                $updata['is_goin'] = 1;
                $updata['into_time'] = time();
                $outstuent_model->save($updata,['id' => $info_student['id']]);
                $this->success('登记成功','',['nickname'=>$info_student['nickname'],'admission_time'=>$info_student['admission_time_text']]);
            }else{
                $this->error('无今日入校申请记录，不允许通行');
            }
        }
        $this -> assign('ids',$ids);
        $this -> assign('gate',input('gate',''));
//        exit(input('gate',''));
        $title = '校外人员入校登记';
        $this -> assign('pageTitle',$title);
        return $this -> fetch('check');
    }


    /*
     * 白名单
     */
//    public function whitelist($mobile){
//        $user = [
//            15954275057 => '张鹏飞',
//            15553212931 => '邵亮',
//            18661903358 => '王骥鹏',
//            18153227097 => '杨书清',
//            15369688313 => '黄小行',
//            17864269853 => '张俊侨',
//            15263040801 => '尹炜光',
//            18660240222 => '田源',
//            13969853293 => '徐青青',
//            18661479555 => '孟昌',
//            15953600109 => '黄梓源',
//            15610022650 => '宋乐兵',
//            18561898313 => '江芍燕',
//            18660288058 => '陈林林',
//            18661473759 => '贾小兵',
//            18669776131 => '任洁',
//            13176526510 => '齐万鑫',
//            13518620485 => '张忠臣',
//            15588986696 => '郑磊',
//            15166026366 => '刘亚伟',
//            // 15063040233 => '王小华',
//            15898849115 => '李雪',
//            15588881913 => '解洪会'
//            // 15665806787 => '孙法林',
//            // 15963242333 => '王颜军',
//            // 15092155738 => '王凤青',
//        ];
//        if (isset($user[$mobile])){
//            $data = [];
//            $data['admission_time'] = date('Y-m-d');
//            $data['nickname'] = $user[$mobile];
//            return $data;
//        }else{
//            return false;
//        }
//    }
    /*
     * 黑名单
     */
    public function blacklist($nickname){
        $user = [
            ['nickname' => '袁绍东'],
            ['nickname' => '魏志敏'],
            ['nickname' => '吴恒涛'],
        ];
        foreach ($user as $k => $v)
        {
            if($v['nickname']==$nickname)
            {
                return false;
            }
        }
        return true;
    }
}
